package com.example.x6.serial;


import android.util.Log;

import com.steven.door.base.SerialCode;
import com.steven.door.manager.ThreadPoolManager;
import com.steven.door.utils.ByteUtils;
import com.steven.door.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class SerialUtil implements SerialCode {
    private SerialPort serialTtyS2;
    private byte[] bytes = new byte[8];
    private static SerialUtil INST;
    private OutputStream ttyS1OutputStream;
    private InputStream ttyS1InputStream;

    public static SerialUtil getInstance() {
        if (INST == null) {
            synchronized (SerialUtil.class) {
                INST = new SerialUtil();
            }
        }

        return INST;
    }

    //打开串口
    public void openSerialPort() {
        try {
            File pathFile = new File(serialPath);
            serialTtyS2 = new SerialPort(pathFile, baudRate, flag);
            ttyS1InputStream = serialTtyS2.getInputStream();
            if (ttyS1InputStream!=null){
                //0.2秒读一次
                //开锁指令
                //锁状态指令
                Runnable readR = new Runnable() {
                    @Override
                    public void run() {
                        while (true) {
                            try {
                                Thread.sleep(200);//0.2秒读一次
                                byte instruct;
                                byte[] readData = readData();
                                if (readData != null) {
                                    instruct = readData[6];
                                    switch (instruct) {
                                        case 0x01://锁是开启状态
                                            EventBus.getDefault().post("open");
                                            break;
                                        case 0x00://锁状关闭状态
                                            EventBus.getDefault().post("close");
                                            break;
                                    }
                                } else {
                                    Log.e("readSerialData", "run: 串口暂时没有数据~");
                                }
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                };
                ThreadPoolManager.getInstance().execute(readR);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void openDoor(byte[] open) {
        try {
            ttyS1OutputStream = serialTtyS2.getOutputStream();
            ttyS1OutputStream.write(open);
            ttyS1OutputStream.flush();
            ttyS1OutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //查询指令
    public void watchSerial(byte[] watch){
        try {
            ttyS1OutputStream = serialTtyS2.getOutputStream();
            ttyS1OutputStream.write(watch);
            ttyS1OutputStream.flush();
            ttyS1OutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    private byte[] readData() {
        try {
            byte[] buffer = new byte[ttyS1InputStream.available()];
            int size = ttyS1InputStream.read(buffer);
            if (size > 0) {
                return buffer;
            } else {
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
